//控制层
app.controller('goodsController', function ($scope, $controller, goodsService,
                                            uploadService, itemCatService, typeTemplateService) {

    $controller('baseController', {$scope: $scope});//继承

    //读取列表数据绑定到表单中  
    $scope.findAll = function () {
        goodsService.findAll().success(
            function (response) {
                $scope.list = response;
            }
        );
    }

    //分页
    $scope.findPage = function (page, rows) {
        goodsService.findPage(page, rows).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    }

    //查询实体
    $scope.findOne = function (id) {
        goodsService.findOne(id).success(
            function (response) {
                $scope.entity = response;
            }
        );
    }

    // //保存
    // $scope.save=function(){
    // 	var serviceObject;//服务层对象
    // 	if($scope.entity.id!=null){//如果有ID
    // 		serviceObject=goodsService.update( $scope.entity ); //修改
    // 	}else{
    // 		serviceObject=goodsService.add( $scope.entity  );//增加
    // 	}
    // 	serviceObject.success(
    // 		function(response){
    // 			if(response.success){
    // 				//重新查询
    // 	        	$scope.reloadList();//重新加载
    // 			}else{
    // 				alert(response.message);
    // 			}
    // 		}
    // 	);
    // }


    //批量删除
    $scope.dele = function () {
        //获取选中的复选框
        goodsService.dele($scope.selectIds).success(
            function (response) {
                if (response.success) {
                    $scope.reloadList();//刷新列表
                    $scope.selectIds = [];
                }
            }
        );
    }

    $scope.searchEntity = {};//定义搜索对象

    //搜索
    $scope.search = function (page, rows) {
        goodsService.search(page, rows, $scope.searchEntity).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    }
//	增加

    $scope.add = function () {
        goodsService.add($scope.entity).success(
            function (response) {
                if (response.success) {
                    alert('保存成功');
                    $scope.entity = {};
                    editor.html('');
                } else {
                    alert(response.message);
                }
            }
        );
    }
//富文本编辑器的内容
    $scope.entity.goodsDesc.introduction = editor.html();

//	图片上传
    $scope.uploadFile = function () {
        uploadService.uploadFile().success(
            function (data) {
                if (data.success) {
                    $scope.image_entity.url = response.message;//设置文件地址
                } else {
                    alert(response.message);
                }
            }).error(function () {
            alert("上传发生错误");
        })
    }

//
    $scope.entity = {goods: {}, goodsDesc: {itemImages: []}};//定义页面实体结构
    //添加图片列表
    $scope.add_image_entity = function () {
        $scope.entity.goodsDesc.itemImages.push($scope.image_entity);
    }
//移除图片
    $scope.remove_image_entity = function (index) {
        $scope.entity.goodsDesc.itemImages.splice(index, 1);
    }

//    读取一级分类
    $scope.selectItemCat1List = function () {
        itemCatService.findByParentId(0).success(function (data) {
            $scope.itemCat1List = data;
        });
    }
//读取二级分类
    $scope.$watch('entity.goods.category1Id', function (newValue, oldValue) {
        //    监控category1Id的值选择二级分类
        itemCatService.findByParentId(newValue).success(function (data) {
            $scope.itemCat2List = data;
        })
    })
//    读取三级分类
    $scope.$watch('entity.goods.category2Id', function (newValue, oldValue) {
        //    监控category1Id的值选择二级分类
        itemCatService.findByParentId(newValue).success(function (data) {
            $scope.itemCat3List = data;
        })
    })
//    读取模板ID
    $scope.$watch('entity.goods.category2Id', function (newValue, oldValue) {
        itemCatService.findOne(newValue).success(function (data) {
            $scope.entity.goods.typeTemplateId = response.typeId;
        })
    })
//选择模板后更新品牌列表
    $scope.$watch('entity.goods.typeTemplateId', function (newValue, oldValue) {
        typeTemplateService.findOne(newValue).success(function (data) {
            $scope.typeTemplate = data;//获取类型模板
            $scope.typeTemplate.brandIds = JSON.parse($scope.typeTemplate.brandIds) //品牌列表
            $scope.entity.goodsDesc.customAttributeItems=JSON.parse( $scope.typeTemplate.customAttributeItems);//扩展属性
        })
    })


});	
